﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;

namespace WXYW
{
    public partial class AllOnProcessApplication : System.Web.UI.Page
    {
        public int RecordsNumber { get; set; }
        public int PageCount { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["PageIndex"] = 0;
                BindProductGridview();
            }
        }

        protected void productGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                ((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();
            }
            if (e.Row.RowType == DataControlRowType.Pager)
            {
                DropDownList pageDDL = e.Row.FindControl("pageDDL") as DropDownList;
                for (int index = 1; index <= PageCount; index++)
                {
                    pageDDL.Items.Add(index.ToString());
                }
                pageDDL.SelectedIndex = Convert.ToInt32(Session["PageIndex"]);
            }
        }

        protected void productGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //if (e.NewPageIndex >= 0 && e.NewPageIndex < productGridView.PageCount)
            //{
            //    productGridView.PageIndex = e.NewPageIndex;
            //}
            
            //BindProductGridview();
        }

        private void BindProductGridview()
        {
            ApplicationService applicationService = new ApplicationService();
            IQueryable<ApplicationForm> forms = applicationService.GetAllProcessingApplicationPaging(keyWordTextBox.Text, Convert.ToInt32(Session["PageIndex"]), 20);
            RecordsNumber = applicationService.GetTotalCountOfApplications(keyWordTextBox.Text.Trim());
            PageCount = RecordsNumber / 20;
            Session["PageCount"] = RecordsNumber / 20;
            productGridView.DataSource = forms;
            productGridView.PageIndex = 0;
            productGridView.DataBind();
        }

        protected void viewLinkButton_click(object sender, EventArgs e)
        {
            string applicationid = ((LinkButton)sender).CommandArgument;
            Response.Redirect(string.Format("ApplicationDetail.aspx?{0}={1}", Constants.QUERYSTR_APPLICATION_ID, applicationid));
        }

        protected void pageDDL_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList pageDDL = sender as DropDownList;
            //productGridView.PageIndex = Convert.ToInt32(pageDDL.SelectedIndex);
            Session["PageIndex"] = pageDDL.SelectedIndex;
            BindProductGridview();
        }

        protected void searchLinkButton_Click(object sender, EventArgs e)
        {
            BindProductGridview();
        }

        protected void resetLinkButton_Click(object sender, EventArgs e)
        {
            keyWordTextBox.Text = "";
            BindProductGridview();
        }

        protected void productGridView_Sorting(object sender, GridViewSortEventArgs e)
        {

            ApplicationService applicationService = new ApplicationService();
            IQueryable<ApplicationForm> products = applicationService.GetAllProcessingApplication(keyWordTextBox.Text);
            RecordsNumber = products.Count();

            string sortDirection = "asc";
            if (Session["OrderDirection"] != null)
            {
                sortDirection = Session["OrderDirection"].ToString();
                if (sortDirection.Equals("asc"))
                {
                    Session["OrderDirection"] = "desc";
                }
                else
                {
                    Session["OrderDirection"] = "asc";
                }
            }
            else
            {
                Session["OrderDirection"] = "asc";
            }

            productGridView.DataSource = products.OrderBy(string.Format("{0} {1}", e.SortExpression, sortDirection));
            productGridView.DataBind();
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Session["PageIndex"] = 0;
            BindProductGridview();
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            if (!Session["PageIndex"].Equals("0"))
            {
                Session["PageIndex"] = Convert.ToInt32(Session["PageIndex"])-1;
            }
            
            BindProductGridview();
        }

        protected void LinkButton3_Click(object sender, EventArgs e)
        {
            if (!Session["PageIndex"].Equals(Convert.ToInt32(Session["PageCount"]) - 1))
            {
                Session["PageIndex"] = Convert.ToInt32(Session["PageIndex"]) +1;
            }
            BindProductGridview();
        }

        protected void LinkButton4_Click(object sender, EventArgs e)
        {
            Session["PageIndex"] = Convert.ToInt32(Session["PageCount"])-1;
            BindProductGridview();
        }
    }
}
